import { BaseScrollPageDto } from '@/common/base/base.dto';
import { ApiProperty, ApiPropertyOptional, PartialType } from '@nestjs/swagger';
import {
  IsArray,
  IsNotEmpty,
  IsString,
  IsUUID,
  IsOptional,
  IsIn,
  IsNumber,
  IsInstance,
  MaxLength,
  IsEnum,
  ArrayMinSize,
  ArrayMaxSize,
} from 'class-validator';
import { Type } from 'class-transformer';
import { BooleanNumber, EarBudsWearingMethod } from 'common/utils/data';

export class EQFiltersDto {
  @ApiProperty()
  @IsNotEmpty()
  @IsIn(['lowpass', 'highpass', 'lowshelf', 'highshelf', 'peaking', 'allpass'])
  filterType: string;

  @ApiProperty()
  @IsNumber()
  frequency: number;

  @ApiProperty()
  @IsNumber()
  q: number;

  @ApiProperty()
  @IsNumber()
  gain: number;
}

export class EQDto {
  @ApiProperty()
  @IsNotEmpty()
  @IsString()
  @MaxLength(500)
  name: string;

  @ApiProperty()
  @IsNotEmpty()
  @IsUUID()
  evaluationUuid: string;

  @ApiProperty()
  @IsNotEmpty()
  @IsString()
  dataGroup: string;

  @ApiProperty()
  @IsNotEmpty()
  @IsArray()
  @IsString({ each: true })
  audioFiles: string[];

  @ApiProperty({
    type: [EQFiltersDto],
  })
  @Type(() => EQFiltersDto)
  @IsArray()
  filters: EQFiltersDto[];

  @ApiPropertyOptional()
  @IsOptional()
  @IsString()
  @MaxLength(500)
  desc?: string;
}

export class EQUpdateDto extends PartialType(EQDto) {
  @IsOptional()
  @ApiPropertyOptional()
  @IsEnum(BooleanNumber)
  open?: BooleanNumber;
}

export class EQIndexListDto extends BaseScrollPageDto {
  @IsOptional()
  @ApiPropertyOptional()
  @IsString()
  keyword?: string;

  @IsOptional()
  @ApiPropertyOptional()
  @IsString()
  uuids?: string;
}

export class EQIndexOpenListDto extends BaseScrollPageDto {
  @IsOptional()
  @ApiPropertyOptional()
  @IsString()
  keyword?: string;
}

export class EQShowOpenDto {
  @IsOptional()
  @ApiPropertyOptional()
  @IsString()
  evaluationUuid?: string;

  @IsOptional()
  @ApiPropertyOptional()
  @IsString()
  dataGroup?: string;
}

export class EQTop10ListDto {
  @ApiProperty({
    description: '频响数据曲线',
    type: [Number],
    isArray: true,
  })
  @IsNotEmpty()
  @IsArray()
  data: number[][];

  @ApiPropertyOptional()
  @IsOptional()
  @IsEnum(EarBudsWearingMethod)
  targetWearingMethod?: EarBudsWearingMethod;
}

export class SegmentItem{
  @ApiPropertyOptional()
  @IsOptional()
  @IsString()
  evaluationUuid?: string;

  @ApiPropertyOptional()
  @IsOptional()
  @IsString()
  dataGroup?: string;

  @ApiPropertyOptional({
    type: [Number],
    isArray: true,
  })
  @IsOptional()
  @IsArray()
  dataSet?: number[][]

  @ApiProperty()
  @IsNotEmpty()
  @IsNumber()
  start: number;

  @ApiProperty()
  @IsNotEmpty()
  @IsNumber()
  end: number;
}

export class EQGetFiltersByTempEQDto {
  @ApiProperty()
  @IsNotEmpty()
  @IsUUID()
  beforeEvaluationUuid: string;

  @ApiProperty()
  @IsNotEmpty()
  @IsString()
  beforeDataGroup: string;

  @ApiPropertyOptional({
    type: [EQFiltersDto],
  })
  @Type(() => EQFiltersDto)
  @IsArray()
  beforeFilters?: EQFiltersDto[];

  @ApiProperty()
  @IsNotEmpty()
  @IsUUID()
  afterEvaluationUuid: string;

  @ApiProperty()
  @IsNotEmpty()
  @IsString()
  afterDataGroup: string;
}

export class EQGetFiltersBySegmentFreqDto {
  @ApiProperty()
  @IsNotEmpty()
  @IsString()
  evaluationUuid: string;

  @ApiProperty()
  @IsNotEmpty()
  @IsString()
  dataGroup: string;

  @ApiProperty({
    type: [SegmentItem],
  })
  @IsNotEmpty()
  @IsArray()
  @Type(() => SegmentItem)
  @ArrayMinSize(1)
  @ArrayMaxSize(5)
  segmentList: SegmentItem[];
}